
import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home/homeView.vue'),
    children: [

      {
        path: '/home/all',
        name: 'all',
        meta: {
          title: '推荐 - 首页'
        },
        component: () => import('../views/home/homechild/allView.vue')
      },
      {
        path: '/home/bird',
        name: 'bird',
        meta: {
          title: '科普 - 首页'
        },
        component: () => import('../views/home/homechild/birdView.vue')
      },
      {
        path: '/home/crawl',
        name: 'crawl',
        meta: {
          title: '种草 - 首页'
        },
        component: () => import('../views/home/homechild/crawlView.vue')
      },
      {
        path: '/home/fish',
        name: 'fish',
        meta: {
          title: '异宠 - 首页'
        },
        component: () => import('../views/home/homechild/fishView.vue')
      },
      {
        path: '/home/lactation',
        name: 'lactation',
        meta: {
          title: '萌宠 - 首页'
        },
        component: () => import('../views/home/homechild/lactationView.vue')
      },
      {
        path: '/home/articleDetail',
        name: 'articleDetail',
        meta: {
          title: '文章详情 - 首页'
        },
        component: () => import('../views/home/homechild/articleDetail.vue')
      },
      {
        path: '/home/userHome',
        name: 'userHome',
        meta: {
          title: '其他用户主页 - 首页'
        },
        component: () => import('../views/home/homechild/userHome.vue')
      },
      {
        path: '/home',
        redirect: {
          name: 'all'
        }
      }
    ]
  },
  // 路由重定向
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/:anywadad',
    redirect: {
      name: 'home'
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/loginView.vue'),
    children: [
      {
        path: '/login/checkuser',
        name: 'checkuser',
        meta: {
          title: '用户登录'
        },
        component: () => import('../views/login/checkUser.vue')
      },
      {
        path: '/login/registeruser',
        name: 'registeruser',
        meta: {
          title: '用户注册'
        },
        component: () => import('../views/login/registerUser.vue')
      },
      {
        path: '/login',
        redirect: {
          name: 'checkuser'
        }
      }
    ]
  },
  {
    path: '/user',
    name: 'user',
    meta: {
      requesth: true
    },
    component: () => import('../views/user/userView.vue'),
    children: [
      {
        path: '/user/myhome',
        name: 'myhome',
        meta: {
          title: '我的个人主页'
        },
        component: () => import('../views/user/userchild/myHome.vue')
      },
      {
        path: '/user/myfans',
        name: 'myfans',
        meta: {
          title: '粉丝列表'
        },
        component: () => import('../views/user/userchild/myFans.vue')
      },
      {
        path: '/user/myfollow',
        name: 'myfollow',
        meta: {
          title: '我的关注'
        },
        component: () => import('../views/user/userchild/myFollow.vue')
      },
      {
        path: '/user/mycare',
        name: 'mycare',
        meta: {
          title: '我的收藏'
        },
        component: () => import('../views/user/userchild/myCollect.vue')
      },
      {
        path: '/user/userdata',
        name: 'userdata',
        meta: {
          title: '我的资料'
        },
        component: () => import('../views/user/userchild/userData.vue')
      },
      {
        path: '/user',
        redirect: {
          name: 'myhome'
        }
      }
    ]
  },

  {
    path: '/video',
    name: 'video',
    component: () => import('../views/video/videoView.vue'),
    children: [
      {
        path: '/video/allvideo',
        name: 'allvideo',
        meta: {
          title: '推荐 - 宠物视频'
        },
        component: () => import('../views/video/videochild/allvideoView.vue')
      },
      {
        path: '/video/follow',
        name: 'follow',
        meta: {
          requesth: true,
          title: '关注 - 宠物视频'
        },
        component: () => import('../views/video/videochild/followView.vue')
      },
      {
        path: '/video/history',
        name: 'history',
        meta: {
          requesth: true,
          title: '历史记录 - 宠物视频'
        },
        component: () => import('../views/video/videochild/historyView.vue')
      },
      {
        path: '/video/hotuser',
        name: 'hotuser',
        meta: {
          title: '视频号 - 宠物视频'
        },
        component: () => import('../views/video/videochild/hotUserView.vue')
      },
      {
        path: '/video/hotlist',
        name: 'hotlist',
        meta: {
          title: '排行榜 - 宠物视频'
        },
        component: () => import('../views/video/videochild/hotListView.vue')
      },

      {
        path: '/video',
        redirect: {
          name: 'allvideo'
        }
      }
    ]
  },
  {
    path: '/videodetail',
    name: 'videodetail',
    meta: {
      title: '视频详情 - 宠物视频'
    },
    component: () => import('../views/detail/videoDetail.vue')

  },
  {
    path: '/complaint',
    name: 'complaint',
    meta: {
      title: '举报'
    },
    component: () => import('../views/complaint/indexView.vue')

  },
  {
    path: '/news',
    name: 'news',
    component: () => import('../views/news/newView.vue'),
    children: [
      {
        path: '/news/fabulous',
        name: 'fabulous',
        meta: {
          requesth: true,
          title: '点赞 - 消息'
        },
        component: () => import('../views/news/newschild/fabulousView.vue')
      },
      {
        path: '/news/comment',
        name: 'comment',
        meta: {
          requesth: true,
          title: '评论 - 消息'
        },
        component: () => import('../views/news/newschild/commentView.vue')
      },
      {
        path: '/news',
        redirect: {
          name: 'fabulous'
        }
      }
    ]
  },
  {
    path: '/serach',
    name: 'serach',
    component: () => import('../views/serach/serachView.vue'),
    children: [
      {
        path: '/serach/article',
        name: 'article',
        meta: {
          title: '文章搜索结果 - 搜索'
        },
        component: () => import('../views/serach/serachchild/articleView.vue')
      },
      {
        path: '/serach/s_user',
        name: 's_user',
        meta: {
          title: '用户搜索结果 - 搜索'
        },
        component: () => import('../views/serach/serachchild/userListView.vue')
      },
      {
        path: '/serach/s_video',
        name: 's_video',
        meta: {
          title: '视频搜索结果 - 搜索'
        },
        component: () => import('../views/serach/serachchild/videoList.vue')
      },
      {
        path: '/serach',
        redirect: {
          name: 'article'
        }
      }
    ]
  },
  {
    path: '/upload',
    name: 'upload',
    meta: {
      requesth: true
    },
    component: () => import('@/views/upload/uploadView.vue'),
    children: [
      {
        path: '/upload/uparticle',
        name: 'uparticle',
        meta: {
          title: '发布文章 - 发布内容',
          requesth: true
        },
        component: () => import('../views/upload/uploadchild/uparticleView.vue')
      },
      {
        path: '/upload/upvideo',
        name: 'upvideo',
        meta: {
          title: '发布视频 - 发布内容',
          requesth: true
        },
        component: () => import('../views/upload/uploadchild/upvideoView.vue')
      },
      {
        path: '/upload',
        redirect: {
          name: 'uparticle'
        }
      }
    ]
  }

]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior (to, from, savedPosition) {
    // 始终滚动到顶部
    return { top: 0 }
  }
})

// 路由拦截
router.beforeEach((to, from, next) => {
  // console.log(from)
  if (to.meta.requesth) {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next({
        path: '/login/checkuser',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

export default router
